package org.shoukaiseki.common.utils;

import com.alibaba.fastjson2.JSONObject;
import org.shoukaiseki.common.text.Convert;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * org.shoukaiseki.common.utils.ServletUtils
 * <br>
 *
 * @author shoukaiseki ブログ http://www.shoukaiseki.top/<br> E-メール jiang28555@Gmail.com<br>
 * @date 2020-12-09 10:27:50<br>
 */
public class ServletUtils {


    public static RequestAttributes getRequestAttributes(){
        //获取RequestAttributes
        return RequestContextHolder.getRequestAttributes();
    }

    public static HttpServletRequest getHttpServletRequest(){
        RequestAttributes requestAttributes = getRequestAttributes();
        //从获取RequestAttributes中获取HttpServletRequest的信息
        return (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
    }

    public static HttpSession getHttpSession(){
        return getHttpServletRequest().getSession();
    }


    /**
     * 将字符串渲染到客户端
     *
     * @param response 渲染对象
     * @param string 待渲染的字符串
     * @return null
     */
    public static String renderString(HttpServletResponse response, String string)
    {
        try
        {
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().print(string);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }



    /**
     * 将对象转为 json string 渲染到客户端
     *
     * @param response 渲染对象
     * @param obj 待渲染的字符串
     * @return null
     */
    public static String renderObjectToJson(HttpServletResponse response, Object obj)
    {
        try
        {
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().print(JSONObject.toJSONString(obj));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }



    /**
     * 获取String参数
     */
    public static String getParameter(String name)
    {
        return getHttpServletRequest().getParameter(name);
    }

    /**
     * 获取String参数
     */
    public static String getParameter(String name, String defaultValue)
    {
        return Convert.toStr(getHttpServletRequest().getParameter(name), defaultValue);
    }

    /**
     * 获取Integer参数
     */
    public static Integer getParameterToInt(String name)
    {
        return Convert.toInt(getHttpServletRequest().getParameter(name));
    }

    /**
     * 获取Integer参数
     */
    public static Integer getParameterToInt(String name, Integer defaultValue)
    {
        return Convert.toInt(getHttpServletRequest().getParameter(name), defaultValue);
    }

}
